1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| #include<bits/stdc++.h> using namespace std; typedef long long s64; #define next nxt const int N = 1005; const int M = 400005; const int INF = 2147483640;
int n; int a[N], b[N], c[N]; s64 dist[N]; int vis[N], q[N], pre[N]; int first[M], go[M], next[M], pas[M], from[M], tot; int pNum[M]; int odd[M],Onum, eve[M],Enum; int S, T, Ans; int prime[M], isp[M], p; s64 Val, w[M];
int get() { int res=1,Q=1;char c; while( (c=getchar())<48 || c>57 ) if(c=='-')Q=-1; res=c-48; while( (c=getchar())>=48 && c<=57 ) res=res*10+c-48; return res*Q; }
void Getp(int MaxN) { for(int i=2; i <= MaxN; i++) { if(!isp[i]) prime[++p] = i; for(int j=1; j <= prime[p] && i*prime[j] <= MaxN; j++) { isp[i * prime[j]] = 1; if(i % prime[j] == 0) break; } } }
int Factor(int x) { int res = 0; while(x != 1) { for(int i=1; i<=p; i++) if(x % prime[i] == 0) { x /= prime[i]; res++; break; } } return res; }
int PD(int x, int y) { if(x > y) swap(x, y); if(x==0 || y==0 || y%x!=0) return 0; x = y / x; for(int i=2; i<x; i++) if(x % i == 0) return 0; return 1; }
void Add(int u, int v, int flow, s64 z) { next[++tot]=first[u]; first[u]=tot; go[tot]=v; pas[tot]=flow; w[tot]=z; from[tot]=u; next[++tot]=first[v]; first[v]=tot; go[tot]=u; pas[tot]=0; w[tot]=-z; from[tot]=v; }
bool Bfs() { for(int i=S; i<=T; i++) dist[i] = -1e18; int tou = 0, wei = 1; q[1] = S; vis[S] = 1; dist[S] = 0; while(tou < wei) { int u = q[++tou]; for(int e=first[u]; e; e=next[e]) { int v=go[e]; if(dist[v] < dist[u] + w[e] && pas[e]) { dist[v] = dist[u] + w[e]; pre[v] = e; if(!vis[v]) { q[++wei] = v; vis[v] = 1; } } } vis[u] = 0; } return dist[T] != -1e18; }
void Deal() { int x = INF; for(int e=pre[T]; e; e=pre[from[e]]) x = min(x, pas[e]); if(Val + dist[T] * x >= 0) { for(int e=pre[T]; e; e=pre[from[e]]) { pas[e] -= x; pas[((e-1)^1)+1] += x; } Val += dist[T] * x; Ans += x; return; } printf("%lld", Ans - Val / dist[T]); exit(0); }
int main() { Getp(M - 5); n = get(); for(int i=1; i<=n; i++) a[i] = get(), pNum[i] = Factor(a[i]); for(int i=1; i<=n; i++) b[i] = get(); for(int i=1; i<=n; i++) c[i] = get();
S = 0; T = n+1; for(int i=1; i<=n; i++) { if(pNum[i] & 1) Add(S,i, b[i],0), odd[++Onum] = i; else Add(i,T, b[i],0), eve[++Enum] = i; }
for(int i=1; i<=Onum; i++) for(int j=1; j<=Enum; j++) { int x = odd[i], y = eve[j]; if( PD(a[x], a[y]) ) Add(x,y, INF,(s64)c[x]*c[y]); }
while(Bfs()) Deal(); printf("%lld", Ans - Val / dist[T]); }
|